package rickyGame.game.action
{
	import flash.geom.Point;
	
	import ricky.game.Actor;
	import ricky.game.action.Act2D;
	import ricky.game.graphic.Graphic;
	
	import ricky.game.Actor2D;
	import rickyGame.game.Fighter;
	import rickyGame.game.GameState;
	import rickyGame.game.SkillObject;
	import rickyGame.game.skill.SkillData;
	
	/**
	 * 创建一个箭支并添加一个行动
	 */
	public class CreateArrow extends Act2D
	{
		public function CreateArrow(actor:Fighter,skill:SkillData,type:String,num:Number=1,target:Actor=null,point:Point=null)
		{
			super(actor);
			_fighter=actor;
			_skill=skill;
			_num=num;
			_typeFly=type;
			this._target=target;
			this._point=point;
		}
		
		override public function begin():void
		{
			_state=1;
			_x=actor.graphic.x;
			_y=actor.graphic.y;
			if(_point)
			{
				_x=_point.x;
				_y=_point.y+5;
			}
			if(_target)
			{
				_x=_target.graphic.x;
				_y=_target.graphic.y+5;
			}
			
			if(_skill)
			{
				createShootArrow();
			}
			
			this.complete();
		}
		
		protected function createShootArrow():void
		{
			var i:int=_num;
			_lastAngle=0;
			while(i>0)
			{
				i--;
				_skillObj=new SkillObject(_fighter,_skill);
				_skillObj.graphic.x=actor.graphic.x;
				_skillObj.graphic.x=actor.graphic.y;
				Actor2D(_skillObj).speed=_skill.parameter1;
				this._skillObj.graphic.wear(_skill.model);
				Graphic(_skillObj.graphic).fps=7;

				var ani:Act2D;
				switch(_typeFly)
				{
					case "line"://直线
						var dx:Number=_x-this._actor.graphic.x;
						var dy:Number=_y-this._actor.graphic.y;
						var angle:Number=Math.atan2(dy,dx);//幅度
						angle=angle*Common.radianToAngle;
						ani=new LineAct(_skillObj,angle,_skill);
						break;
					case "angleline"://角度
						var angel:Number=_fighter.direction*45-130;//方向
						var per:Number= 100/_num;
						var realangel:Number=angel+per*_lastAngle;
						ani=new LineAct(_skillObj,realangel,_skill);
						_lastAngle++;
						break;
					case "follow":
						ani=new FollowHitAct(_skillObj,_target,_skill);
						break;
				}
				
				if(ani)
					_skillObj.addAct(ani);
				
				GameState.inst.sceneManager.addGameObject(_skillObj);
			}
		}
		
		
		//private
		
		private var _x:Number;
		private var _y:Number;//目标y位置
		private var _fighter:Fighter;
		private var _num:Number;//创建的数量
		private var _typeFly:String;//飞行方式
		private var _lastAngle:Number=-20;//上个角度
		private var _point:Point;//目标点
		private var _target:Actor;//目标
		private var _skill:SkillData;//技能数据
		private var _skillObj:SkillObject;//技能对象
	}
}